import axios from 'axios'
import {notification} from 'ant-design-vue'

let baseURL = '';

if(process.env.VUE_APP_MODE === 'dev'){
  baseURL = process.env.VUE_APP_API_URL;
}else if(process.env.VUE_APP_MODE === 'prod'){
  baseURL = process.env.VUE_APP_API_URL;
}

// 创建 axios 实例
const service = axios.create({
  baseURL: baseURL, //设置请求地址
  timeout: 60000 // 请求超时时间
});

const err = (error) => {
  if (error.response) {
    let data = error.response.data;
    console.log("------异常响应------",error.response.status);
    switch (error.response.status) {
      case 403:
        notification.error({ message: '系统提示', description: '拒绝访问',duration: 4});
        break;
      case 404:
        notification.error({ message: '系统提示', description:'很抱歉，资源未找到!',duration: 4});
        break;
      case 504:
        notification.error({ message: '系统提示', description: '网络超时'});
        break;
      default:
        notification.error({
          message: '系统提示',
          description: data.message,
          duration: 4
        });
        break;
    }
  }
  return Promise.reject(error);
};

// 添加请求拦截器
service.interceptors.request.use(config => {
  //这里设置请求头
  return config;
},(error) => {
  return Promise.reject(error)
});

// 添加响应拦截器
service.interceptors.response.use(response => {
  return response.data;
}, err);

export {
  service as axios
}





